/**
 * create by young (young2f@outlook.com) on 2020/12/4
 */
'use strict';
const base = require('../base/controllerBase');

class DynamicLabelController extends base {
  async createDynamicLabel() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        table: { type: 'string' },
        createUser: { type: 'string' },
        createUsername: { type: 'string' },
        required: { type: 'boolean' },
        //title: { type: 'string' },
        key: { type: 'string' },
        inputType: { type: 'string' },
        type: { type: 'string' },
      };
      console.log('create label', body);
      ctx.validate(model, body);
      const result = await service.dLabelsService.create(body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      console.log('create label catch', e);
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findDynamicLabels() {
    const { ctx, } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      ctx.validate(model, body);

      const result = await ctx.model.DLabel.find(body);
      console.log("find d label all",result);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteDynamicLabel() {
    const { ctx, } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' }, key: { type: 'string' }, table: { type: 'string' },
      };
      ctx.validate(model, body);
      const result = await ctx.model.DLabel.deleteMany(body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateDynamicLabel() {
    const { ctx, } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' }, key: { type: 'string' }, table: { type: 'string' },
      };
      ctx.validate(model, body);
      const { tenantCode, key, table, tabsCode } = body;

      const result = await ctx.model.DLabel.updateOne({ tenantCode, key, table, tabsCode }, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async getDynamicLabels() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' }, table: { type: 'string' },
      };
      ctx.validate(model, body);
      ctx.body = await service.dLabelsService.getDLabelTableColumns(body);
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }
}

module.exports = DynamicLabelController;
